package Csv2RDF;

import Json2RDF.Json2Java.OntologyClasses.Face;
import org.apache.jena.rdf.model.Property;

import java.util.*;

/**
 * The {@code RelationExtraction} class provides functionality for extracting and establishing
 * relationships between Face objects based on input from a CSV data structure.
 * Relationships are defined in the CSV, where each cell indicates the type of relationship
 * between two faces. This class handles the conversion of CSV data into bidirectional
 * relations between Face instances.
 */
public class RelationExtraction {


  /**
   * Extracts and sets relationships between faces based on a given CSV array.
   * Each row in the CSV array corresponds to relationships between a "face" and other faces.
   *
   * @param csvArray a 2D array representing the CSV data where each row and column define relationships between faces
   * @param faceMap a map containing face identifiers as keys and Face objects as values
   */
  public static void relationExtraction(String[][] csvArray, Map<String, Face> faceMap) {
    // Loop through each row starting from index 1 (skip header row)
    for (int rowIndex = 1; rowIndex < csvArray.length; rowIndex++) {
      String[] csvRow = csvArray[rowIndex];
      Face face1 = faceMap.get(csvRow[0]);
      if (face1 == null) continue;          // Skip if face1 is not found in the map

      // Loop through each column in the row, starting from index 1
      for (int colIndex = 1; colIndex < csvRow.length; colIndex++) {
        String csvCol = csvRow[colIndex];
        if (csvCol.isEmpty()) continue;     // Skip if the cell is empty

        Face face2 = faceMap.get(csvArray[0][colIndex]);
        if (face2 == null) continue;        // Skip if face2 is not found in the map

        // Convert string representation to list of properties and get inverse properties
        List<Property> propertyList = String2Property.string2Property(csvCol);
        List<Property> inversePropertyList = RelationTrans.relationInv(propertyList);

        // Add the relationships between face1 and face2
        face1.addRelation(face2, propertyList);
        face2.addRelation(face1, inversePropertyList);
      }
    }
  }

}

